package cn.pet_know.service.impl;

import cn.pet_know.common.constants.CircleStatus;
import cn.pet_know.common.constants.DelFlag;
import cn.pet_know.common.entity.vo.UserVO;
import cn.pet_know.common.feigns.UserFeign;
import cn.pet_know.common.my_exception.CustomException;
import cn.pet_know.common.result.R;
import cn.pet_know.common.utils.ConverterUtils;
import cn.pet_know.common.utils.GenIdUtil;
import cn.pet_know.dao.mapper.PetCircleMapper;
import cn.pet_know.entity.PetCircle;
import cn.pet_know.entity.PetCricleImgs;
import cn.pet_know.entity.dto.PetCircleDTO;
import cn.pet_know.entity.vo.PetCircleVO;
import cn.pet_know.service.PetCircleService;
import cn.pet_know.service.PetCricleImgsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class PetCircleServiceImpl extends ServiceImpl<PetCircleMapper, PetCircle> implements PetCircleService {

    @Resource
    private PetCricleImgsService petCricleImgsService;
    @Resource
    private UserFeign userFeign;

    @Override
    public Boolean toAdd(PetCircleDTO petCircleDTO) {
        // 发布宠圈信息
        if (Objects.isNull(petCircleDTO.getCircleTitle())) {
            throw new CustomException("circleTile is null");
        }
        if (Objects.isNull(petCircleDTO.getCircleText())) {
            throw new CustomException("circleText is null");
        }
        if (Objects.isNull(petCircleDTO.getCircleType())) {
            throw new CustomException("circleType is null");
        }

        // 设置id
        long uid = GenIdUtil.generatorId();
        petCircleDTO.setCircleId(uid);
        petCircleDTO.setCircleStatus(CircleStatus.UNAUDITED);

        // 取出图片列表地址
        List<String> valueAddress = petCircleDTO.getValueAddress();
        // 封装图片对象,写入数据库
        ArrayList<PetCricleImgs> arrayList = new ArrayList();
        for (String address : valueAddress) {
            PetCricleImgs petCricleImgs = new PetCricleImgs();
            petCricleImgs.setImgId(GenIdUtil.generatorId());
            petCricleImgs.setUrl(address);
            petCricleImgs.setCircleId(uid);
            arrayList.add(petCricleImgs);
        }
        petCricleImgsService.saveBatch(arrayList);

        // 存储内容
        PetCircle petCircle = ConverterUtils.copyR(petCircleDTO, new PetCircle());
        return this.save(petCircle);
    }

    @Override
    public Boolean toDel(PetCircleDTO petCircleDTO) {
        if (Objects.isNull(petCircleDTO.getCircleId())) {
            throw new CustomException("circleId is null");
        }
        return this.update(new LambdaUpdateWrapper<PetCircle>().eq(PetCircle::getCircleId, petCircleDTO.getCircleId()).set(PetCircle::getDeleted, DelFlag.DELETED));
    }

    @Override
    public Boolean toUp(PetCircleDTO petCircleDTO) {
        return null;
    }

    @Override
    public Boolean toReview(PetCircleDTO petCircleDTO) {
        if (Objects.isNull(petCircleDTO.getCircleId())) {
            throw new CustomException("circleId is null");
        }
        return this.update(
                new LambdaUpdateWrapper<PetCircle>()
                        .eq(PetCircle::getCircleId, petCircleDTO.getCircleId())
                        .set(PetCircle::getCircleStatus, petCircleDTO.getCircleStatus())
                        .set(PetCircle::getCircleMessage, petCircleDTO.getCircleMessage() == null ? "" : petCircleDTO.getCircleMessage())
        );
    }

    @Override
    public IPage<PetCircleVO> toPage(PetCircleDTO petCircleDTO) {
        Page<PetCircle> petCirclePage = new Page<>();
        petCirclePage.setSize(petCircleDTO.getPageSize());
        petCirclePage.setCurrent(petCircleDTO.getPageNo());

        // 查询
        LambdaQueryWrapper<PetCircle> petCircleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        petCircleLambdaQueryWrapper.eq(PetCircle::getDeleted, DelFlag.EXISTS);
        petCircleLambdaQueryWrapper.eq(Objects.nonNull(petCircleDTO.getCircleTitle()), PetCircle::getCircleTitle, petCircleDTO.getCircleTitle());
        petCircleLambdaQueryWrapper.eq(Objects.nonNull(petCircleDTO.getUserId()), PetCircle::getUserId, petCircleDTO.getUserId());
        petCircleLambdaQueryWrapper.eq(Objects.nonNull(petCircleDTO.getCircleType()), PetCircle::getCircleType, petCircleDTO.getCircleType());
        petCircleLambdaQueryWrapper.orderByDesc(PetCircle::getCreatedAt);
        if (Objects.nonNull(petCircleDTO.getStartTime()) && Objects.nonNull(petCircleDTO.getEndTime())) {
            petCircleLambdaQueryWrapper.between(PetCircle::getCreatedAt, petCircleDTO.getStartTime(), petCircleDTO.getEndTime());
        }
        Page<PetCircleVO> petCircleVOPage = ConverterUtils.pageCopyR(this.page(petCirclePage, petCircleLambdaQueryWrapper), PetCircleVO.class);

        List<Long> idList
                = petCircleVOPage.getRecords().stream().map(PetCircleVO::getCircleId).collect(Collectors.toList());

        List<Long> userIdList
                = petCircleVOPage.getRecords().stream().map(PetCircleVO::getUserId).collect(Collectors.toList());
        R<List<UserVO>> user = userFeign.findUserList(userIdList);
        List<UserVO> data = user.getData();
        // 查询图片地址封装并返回
        List<PetCricleImgs> list = petCricleImgsService.list(new LambdaQueryWrapper<PetCricleImgs>().in(PetCricleImgs::getCircleId, idList));
        for (PetCircleVO circleVO : petCircleVOPage.getRecords()) {
            ArrayList<String> imgs = new ArrayList<>();
            for (PetCricleImgs petCricleImgs : list) {
                if (circleVO.getCircleId().equals(petCricleImgs.getCircleId())) {
                    imgs.add(petCricleImgs.getUrl());
                }
            }
            circleVO.setValueAddress(imgs);
            for (UserVO userVO : data) {
                if (circleVO.getUserId().equals(userVO.getUserId())) {
                    circleVO.setUserVO(userVO);
                }
            }
        }
        return petCircleVOPage;
    }
}